package com.haiya.identityaccess.infrastructure.persistence.mybatis.repository;

import com.haiya.identityaccess.domain.identity.model.Tenant;
import com.haiya.identityaccess.domain.identity.model.TenantId;
import com.haiya.identityaccess.domain.identity.repository.TenantRepository;
import com.haiya.identityaccess.infrastructure.persistence.mybatis.datamodel.DataTenant;
import com.haiya.identityaccess.infrastructure.persistence.mybatis.mapper.TenantMapper;
import com.haiya.identityaccess.infrastructure.persistence.mybatis.translation.TenantTranslate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.UUID;

/**
 * @author qiaoguoqiang
 */
@Repository
public class TenantRepositoryImpl implements TenantRepository {

    @Autowired
    private TenantMapper tenantMapper;

    @Override
    public void add(Tenant aTenant) {
        DataTenant dataTenant = TenantTranslate.toDataEntity(aTenant);
        tenantMapper.insert(dataTenant);
    }

    @Override
    public TenantId nextIdentity() {
        return new TenantId(UUID.randomUUID().toString().toUpperCase());
    }

    @Override
    public void remove(Tenant aTenant) {
        tenantMapper.remove(aTenant.tenantId().id());
    }

    @Override
    public Tenant tenantNamed(String aName) {
        DataTenant dataTenant = tenantMapper.tenantNamed(aName);
        return TenantTranslate.toDomainEntity(dataTenant);
    }

    @Override
    public Tenant tenantOfId(TenantId aTenantId) {
        DataTenant dataTenant = tenantMapper.tenantOfId(aTenantId.id());
        Tenant tenant = TenantTranslate.toDomainEntity(dataTenant);
        return tenant;
    }
}
